package com.hb.hb_casefile;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import com.core.*;

/**
 * Data access object (DAO) for domain model class HbCasefile.
 * @see com.hb.hb_casefile.HbCasefile
 * @author MyEclipse - Hibernate Tools
 */
public class HbCasefileDAO extends BaseHibernateDAO {

    private static final Log log = LogFactory.getLog(HbCasefileDAO.class);

	//property constants
	public static final String CASEID = "caseid";
	public static final String FILENAME = "filename";
	public static final String FILEPATH = "filepath";
	
	Transaction tx=null;
    
    public void save(HbCasefile transientInstance) {
        log.debug("saving HbCasefile instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().save(transientInstance);
            tx.commit();
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(HbCasefile persistentInstance) {
        log.debug("deleting HbCasefile instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().delete(persistentInstance);
            tx.commit();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public HbCasefile findById( java.lang.Integer id) {
        log.debug("getting HbCasefile instance with id: " + id);
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            HbCasefile instance = (HbCasefile) getSession()
                    .get("com.hb.hb_casefile.HbCasefile", id);
            tx.commit();
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(HbCasefile instance) {
        log.debug("finding HbCasefile instance by example");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            List results = getSession()
                    .createCriteria("com.hb.hb_casefile.HbCasefile")
                    .add(Example.create(instance))
            .list();
            tx.commit();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding HbCasefile instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from HbCasefile as model where model." 
         						+ propertyName + "= ?";
         tx=getSession().getTransaction();
     	tx.begin();
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 tx.commit();
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByCaseid(Object caseid) {
		return findByProperty(CASEID, caseid);
	}
	
	public List findByFilename(Object filename) {
		return findByProperty(FILENAME, filename);
	}
	
	public List findByFilepath(Object filepath) {
		return findByProperty(FILEPATH, filepath);
	}
	
    public HbCasefile merge(HbCasefile detachedInstance) {
        log.debug("merging HbCasefile instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            HbCasefile result = (HbCasefile) getSession()
                    .merge(detachedInstance);
            tx.commit();
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(HbCasefile instance) {
        log.debug("attaching dirty HbCasefile instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().saveOrUpdate(instance);
            tx.commit();
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(HbCasefile instance) {
        log.debug("attaching clean HbCasefile instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().lock(instance, LockMode.NONE);
            tx.commit();
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}